package com.motion.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.motion.dto.BannerDto;
import com.motion.dto.CourseClassDto;
import com.motion.generator.entity.Course;
import com.motion.generator.entity.CourseClass;
import com.motion.result.Result;
import com.motion.result.ResultFactory;
import com.motion.service.CourseClassService;
import com.motion.service.CourseService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.util.*;
import java.util.List;
@RestController
@RequestMapping("/api/CourseClass")
@Api("课程分类模块")
public class CourseClassController {
    @Autowired
    private CourseClassService courseClassService;
    @Autowired
    private CourseService courseService;
    @Value("${homePage.courseClassIdList}")
    private String courseClassIdList;
    private String bannerNameList="篮球;足球;羽毛球;网球";
    @Value("${homePage.bannerUrlList}")
    private String bannerUrlList;
    //
    @RequestMapping(value = "/getSorts", method = RequestMethod.GET)
    public Result getSorts(){
        List<CourseClass> courseClassList=courseClassService.AllcourseClasses();
        HashMap<String, CourseClassDto> hashMap = new HashMap<>();
        int num=1;
        for(CourseClass item : courseClassList){
            String name = item.getClassName();
            if (!hashMap.containsKey(name)){
                CourseClassDto courseClassDto = new CourseClassDto();
                courseClassDto.setId(num++);
                courseClassDto.setName(name);
                courseClassDto.setTab(new ArrayList<CourseClass>());
                hashMap.put(name, courseClassDto);
            }
            if(!item.getClassValue().equals("全部")) {
                CourseClassDto courseClassDto = hashMap.get(name);
                List<CourseClass> courseClasses1 = courseClassDto.getTab();
                courseClasses1.add(item);
                courseClassDto.setTab(courseClasses1);
                hashMap.put(name, courseClassDto);
            }else{
                CourseClassDto courseClassDto = hashMap.get(name);
                courseClassDto.setId(item.getCourseClassId().intValue());
                hashMap.put(name, courseClassDto);
            }
        }
        List<CourseClassDto> result = new LinkedList<>();
        for(String key: hashMap.keySet()){
            result.add(hashMap.get(key));
        }
        return ResultFactory.buildSuccessResult(result);
    }

    //分类条件返回
    @RequestMapping(value = "/getCourseClass", method = RequestMethod.GET)
    public Result getCourseClass(@RequestParam List<String> name) {
        List<List<CourseClass>> result=new ArrayList<List<CourseClass>>();
        for ( String item:name){
            result.add(courseClassService.str2CourseClassList(item));
        }
        return ResultFactory.buildSuccessResult(result);
    }

    @RequestMapping(value = "/getCourseClassList", method = RequestMethod.GET)
    public Result getCourseClassList(@RequestParam List<String> name) {
        List<List<CourseClass>> data=courseClassService.nameToValue(name);
        return ResultFactory.buildSuccessResult(data);
    }

    @RequestMapping(value = "/getSpecialSort", method = RequestMethod.GET)
    public Result getSpecialSort(){
        List<Long> longList=courseService.StringToListLong(courseClassIdList);
        List<CourseClass> courseClassList=courseClassService.id2CourseClass(courseService.StringToListLong(courseClassIdList));
        List<String> stringList= Arrays.asList(bannerNameList.split(";"));
        List<String> urlList= Arrays.asList(bannerUrlList.split(";"));
        return ResultFactory.buildSuccessResult(courseClassService.getBannerList(courseClassList,stringList,urlList));
    }


    @RequestMapping(value = "/changeSpecialSort", method = RequestMethod.GET)
    public Result changeSpecialSort(@RequestParam String idStr,@RequestParam String nameStr,@RequestParam String urlStr){
        courseClassIdList=idStr;
        bannerNameList=nameStr;
        bannerUrlList=urlStr;
        return ResultFactory.buildSuccessResult("成功");
    }
}
